// 文件处理
// 格式化上传文件
import excelImg from "@/images/excel.png";
import wordImg from "@/images/word.png";
import pdfImg from "@/images/pdf.png";
import rarImg from "@/images/rar.png";
import zipImg from "@/images/zip.png";
import fileImg from "@/images/file.png";

// 格式化上传文件
export const formatUploadFile = (fileList: any) => {
  const _list = fileList.map((item: any) => {
    if (item.response && item.response !== undefined && item.response != null) {
      return `${item.name}##${item.response.data.img}`;
    } else {
      return `${item.name}##${item.oldImg}`;
    }
  });

  return _list.join("@@");
};
// 返回旧文件数据
export const returnOldFiles = (list: any) => {
  const _oldFileList: any = [];
  if (list && list.length > 0) {
    list.forEach((item: any) => {
      _oldFileList.push({
        name: item.name,
        oldImg: item.oldImg,
        url: item.img,
      });
    });
  }
  return _oldFileList;
};

// 返回显示的文件
export const returnFileExtension = (fileItemList: any) => {
  const imgItemList: any = [],
    previewImglist: any = [],
    otherItemList: any = [];
  if (fileItemList && fileItemList.length > 0) {
    const imgExtensionList = ["jpg", "jpeg", "png"]; // 图片格式后缀
    fileItemList.forEach((item: any) => {
      const _extension = item.img
        .substring(item.img.lastIndexOf(".") + 1)
        .toLowerCase();
      item.extension = _extension;
      item.extensionImg = "";
      if (imgExtensionList.indexOf(_extension) < 0) {
        if (_extension === "xlsx" || _extension === "xls") {
          item.extensionImg = excelImg;
        } else if (_extension === "doc" || _extension === "docx") {
          item.extensionImg = wordImg;
        } else if (_extension === "pdf") {
          item.extensionImg = pdfImg;
        } else if (_extension === "rar") {
          item.extensionImg = rarImg;
        } else if (_extension === "zip") {
          item.extensionImg = zipImg;
        } else {
          item.extensionImg = fileImg;
        }
        otherItemList.push(item);
      } else {
        previewImglist.push(item.img);
        imgItemList.push(item);
      }
    });
  }
  return {
    imgItemList,
    otherItemList,
    previewImglist,
  };
};
